package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbUserBankDao;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.redis.util.RedisUtil;
import com.framework.service.DeptSmsService;
import com.framework.service.PaymentService;
import com.framework.service.yhbservice.YhbUserBankService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.bank.BankUtil;
import com.framework.validator.Assert;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.user.YhbUserBank;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author wq
 */
@Service("yhbUserBankService")
public class YhbUserBankServiceImpl extends ServiceImpl<YhbUserBankDao, YhbUserBank> implements YhbUserBankService {
    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private PaymentService paymentService;
    @Autowired
    private DeptSmsService deptSmsService;


    @Override
    public YhbUserBank queryDefaultBankByToken(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        String userId = user.getUserId();
        YhbUserBank temp = queryBankInfoByUserId(userId);
        if (temp == null) {
            throw new RRException("9000", "未查到银行卡信息");
        }
        return temp;
    }

    @Override
    public void insertBank(YhbUserBank bank, String vCode, String token, String password, String confPassword, String identity) {
        Assert.isNull(bank, "参数错误");
        if(password!=null && !"".equals(password) && confPassword!=null &&!"".equals(confPassword)){
            yhbUserService.setTradePassword(token,password,confPassword);
        }

        if (StringUtils.isBlank(bank.getBankNo()) ||
                StringUtils.isBlank(bank.getCardName()) ||
                StringUtils.isBlank(bank.getPhone()) ||
                StringUtils.isBlank(vCode) ||
                StringUtils.isBlank(token) ||
                bank.getIsDefault() == null ||
                bank.getIsDefault() == 0
        ) {
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUser user = yhbUserService.queryUserByToken(token);
        if(identity!=null && !"".equals(identity)){
            yhbUserService.updateIdentity(user,identity);
        }
        Long deptId = user.getDeptId();

        YhbUserBank bankInfo = querybankByNo(bank.getBankNo(), deptId);
        if (bankInfo != null) {
            throw new RRException("9000", "此卡号已经存在");
        }
        deptSmsService.checkSms( "i_bank", bank.getPhone(),vCode);
        bank.setCreateDate(new Date());
        //如果银行卡名称为空
        if(bank.getBankName()==null || bank.getBankName().length()==0){
            Map<String, Object> bankMap = BankUtil.getBankCodeAndName(bank.getBankNo());
            String bankName = (String) bankMap.get("bankName");
            bank.setBankName(bankName);
        }
        YhbUserBank b = queryBankInfoByUserId(user.getUserId());
        if(b==null){
            bank.setIsDefault(1L);
        }

        bank.setUserId(user.getUserId());
        bank.setDeptId(deptId);
        baseMapper.insert(bank);
        //清除缓存
        redisUtil.del("i_bank_" + bank.getPhone());
        //查询是否添加成功
        YhbUserBank yhbUserBank = querybankByNo(bank.getBankNo(), deptId);
        if (yhbUserBank == null) {
            throw new RRException("9000", "银行卡信息添加失败");
        }
        //设置成默认
        if (bank.getIsDefault() == 1) {
            setDefault(yhbUserBank.getYhbBankId());
        }
    }
    @Override
    public YhbUserBank querybankByNo(String bankNo, Long deptId) {
        YhbUserBank b = new YhbUserBank();
        b.setBankNo(bankNo);
        b.setDeptId(deptId);
        b.setIsShow(1L);
        return baseMapper.selectOne(b);

    }

    @Override
    public void setDefault(Long yhbBankId) {
        YhbUserBank bank = new YhbUserBank();
        bank.setYhbBankId(yhbBankId);
        YhbUserBank sysBank = baseMapper.selectOne(bank);
        if (sysBank != null) {
            String userId = sysBank.getUserId();
            baseMapper.updateDefaultByAgentId(userId);
            baseMapper.isDefaultById(yhbBankId);
        }


    }



    @Override
    public void updateBank(YhbUserBank bank, String vCode, String token) {
        Assert.isNull(bank, "参数错误");
        if (StringUtils.isBlank(bank.getBankNo()) ||
                StringUtils.isBlank(bank.getCardName()) ||
                StringUtils.isBlank(bank.getPhone()) ||
                StringUtils.isBlank(vCode) ||
                StringUtils.isBlank(token) ||
                bank.getYhbBankId() == null ||
                bank.getYhbBankId() == 0 ||
                bank.getIsDefault() == null ||
                bank.getIsDefault() == 0
        ) {
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUser user = yhbUserService.queryUserByToken(token);
        Long deptId = user.getDeptId();
        //对银行卡号校验是否存在
        YhbUserBank userBank = infoBank(token, bank.getYhbBankId());
        String bankNo = userBank.getBankNo();
        if (!StringUtils.equals(bankNo, bank.getBankNo())) {
            YhbUserBank bankInfo = querybankByNo(bank.getBankNo(), deptId);
            if (bankInfo != null) {
                throw new RRException("9000", "此卡号已经存在");
            }
        }
        deptSmsService.checkSms( "u_bank", bank.getPhone(),vCode);
        //查询所属的支付信息,是否需要银行行号
        SysPayment payment = paymentService.queryPaymentByDeptId(deptId);
        if (payment == null) {
            throw new RRException("9000", "未查询到签约的·提现信息");
        }

        baseMapper.updateById(bank);
        //清除缓存
        redisUtil.del("u_bank_" + bank.getPhone());
        //设置成默认
        if (bank.getIsDefault() == 1) {
            setDefault(bank.getYhbBankId());
        }
    }

    @Override
    public YhbUserBank infoBank(String token, Long yhbBankId) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        YhbUserBank yhbUserBank = new YhbUserBank();
        yhbUserBank.setYhbBankId(yhbBankId);
        YhbUserBank b = baseMapper.selectOne(yhbUserBank);
        Assert.isNull(b, "未查询到银行卡信息");
        if (!b.getUserId().equals(user.getUserId())) {
            throw new RRException("9000", "此银行卡不属于当前登录人");
        }
        return b;
    }

    @Override
    public void delBank(Long bankId) {
        if (bankId == null ||
                bankId.equals(0L)) {
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUserBank b = baseMapper.selectById(bankId);
        b.setIsShow(2L);
        baseMapper.updateById(b);
    }

    @Override
    public List<YhbUserBank> queryBankListByToken(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        List<YhbUserBank> list = baseMapper.selectList(new EntityWrapper<YhbUserBank>().eq("user_id", user.getUserId())
                .eq("is_show", 1)
        );
        return list;
    }

    @Override
    public YhbUserBank queryBankInfoByUserId(String userId) {
        YhbUserBank yhbUserBank = new YhbUserBank();
        yhbUserBank.setUserId(userId);
        yhbUserBank.setIsDefault(1L);
        yhbUserBank.setIsShow(1L);
        YhbUserBank temp = baseMapper.selectOne(yhbUserBank);

        return temp;
    }

    @Override
    public Map<String, Object> queryBankNameByNo(String bankNo) {
        Map<String, Object> map = BankUtil.getBankCodeAndName(bankNo);
        return map;
    }

    @Override
    public YhbUserBank selectOneBank(String userId) {
        YhbUserBank b=baseMapper.selectOneBank(userId);
        return b;
    }

}
